#include "StdAfx.h"
#include "face_img_download_thread.h"
#include <interface/interface.h>
#include <commondef/glb_msg_define.h>
#include <utility/api_tools.h>
#include "dll_object.h"

face_img_download_thread::face_img_download_thread(void)
{
	this->session_ = NULL;
}

face_img_download_thread::~face_img_download_thread(void)
{
	if (this->session_)
	{
		this->session_->destroy();
	}
}

void face_img_download_thread::OnProcMsgQueueStart()
{
}

void 
face_img_download_thread::LoopProc(download_img_pack* pMsgPack, PWORKER_PARAMETER work_para)
{
	while ( !g_dll_obj->is_plugings_registed_ )
	{
		Sleep(10);
	}

	CString user_id = pMsgPack->user_id;
	aot::tt::iobj_auto_ptr<aot::data::iuser_info> user_info;
	if ( !get_global()->user_lst_mgr_->get_user_info(user_id, user_info) )
	{
		return;
	}

	if ( user_info->face_url->length() <= 0 || user_info->face_custom->length()<= 0)
	{
		return;
	}

	CString face_img = user_info->face_custom->c_str();
	CString face_img_b = face_img;
	int pos = face_img.ReverseFind('_');
	if ( pos >= 0 )
	{
		face_img.Insert(pos+1, 'a');
		face_img_b.Insert(pos+1, 'b');
	}
	else
	{
		return;
	}

	CString image_path = this->face_img_folder_ + face_img;
	if ( ::GetFileAttributes(image_path) != -1)
	{
		return;
	}
	else
	{
		CString face_url = user_info->face_url->c_str();
		face_url += "\\" + face_img;
		this->session_->set_url(face_url);
		this->session_->set_file_name(image_path);
		if ( this->client_->syn_process( this->session_) )
		{
			if ( pMsgPack->from_dll.IsEmpty() )
			{
				aot::tt::istr* id_str = new aot::tt::str_impl;
				id_str->assign(user_id);
				aot::tt::sys_event_param_t param;
				param.wparam = id_str;
				param.lparam = 0;
				g_dll_obj->im_->dispatch_sys_event(aot::tt::imain::regist_sys_event_t::e_type_ui, 
					WM_GLB_UI_REFRESH_CUSTOM_FACE_IMG,  (WPARAM)id_str, 0, &param);
			}
			else
			{

				aot::tt::command_t cmd;
				aot::tt::result_t r;
				cmd.type = WM_GLB_UI_REFRESH_CUSTOM_FACE_IMG;
				cmd.user_data_ptr[0] = (void *)(LPCSTR)pMsgPack->user_id;
				cmd.user_data[0] = 0;
				g_dll_obj->im_->command_to(pMsgPack->from_dll, &cmd, &r);
			}
		}
		else
		{
			AOT_TRACE(AOT_LM_ERROR, "download custom face image_a failed, file_name:", face_img);
		}
		
		image_path = this->face_img_folder_ + face_img_b;
		face_url = user_info->face_url->c_str();
		face_url += "\\" + face_img_b;
		this->session_->set_url(face_url);
		this->session_->set_file_name(image_path);
		if ( this->client_->syn_process( this->session_) )
		{
			if ( pMsgPack->from_dll.IsEmpty() )
			{
				//aot::tt::istr* id_str = new aot::tt::str_impl;
				// id_str->assign(user_id);
				//g_dll_obj->im_->dispatch_sys_event(aot::tt::imain::regist_sys_event_t::e_type_ui, 
				//	WM_GLB_UI_REFRESH_CUSTOM_FACE_IMG,  (WPARAM)id_str, 1);
			}
			else
			{
				aot::tt::command_t cmd;
				aot::tt::result_t r;
				cmd.type = WM_GLB_UI_REFRESH_CUSTOM_FACE_IMG;
				cmd.user_data_ptr[0] = (void *)(LPCSTR)pMsgPack->user_id;
				cmd.user_data[0] = 1;
				g_dll_obj->im_->command_to(pMsgPack->from_dll, &cmd, &r);
			}
		}
		else
		{
			AOT_TRACE(AOT_LM_ERROR, "download custom face image_b failed, file_name:", face_img);
		}
	}
}


BOOL 
face_img_download_thread::push_message(const char* user_id, const char* from_dll)
{
	if ( user_id == NULL || user_id[0] == 0 )
	{
		return FALSE;
	}

	download_img_pack *ptr_pack = new download_img_pack;
	ptr_pack->user_id = user_id;
	ptr_pack->from_dll = from_dll;
	Add(ptr_pack);
	return TRUE;
}

BOOL 
face_img_download_thread::Create( int nThreadCount /*= 1*/ )
{
	this->face_img_folder_ = get_main_obj()->get_curr_user_path();
	this->face_img_folder_ += "\\visualize\\";

	if ( !g_dll_obj->interface_.create_file_trans_client(this->client_))
	{
		return FALSE;
	}
	
	this->client_->create_session(&this->session_);
	this->session_->set_type(aot::file_trans::isession::e_direct_download_file);

	return CMsgQueueExThreads::Create(nThreadCount);
}

/// basic check by fan05